import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class OddEvenBlockingQueue {
	private static final int TIME_DELAY = 1;

	private final ExecutorService executor;

	public OddEvenBlockingQueue() {
		BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(2);

		executor = new ThreadPoolExecutor(2, 2, TIME_DELAY, TimeUnit.SECONDS,
				queue);
	}

	public void submitTask(Runnable task) {
		executor.submit(task);
	}

	public static void main(String[] args) {
		Runnable odd = new Counter(1, "OddThread");
		Runnable even = new Counter(2, "EvenThread");
		OddEvenBlockingQueue app = new OddEvenBlockingQueue();
		app.submitTask(odd);
		app.submitTask(even);
	}
}

class Counter implements Runnable {
	private int counter;
	private String threadName;

	public Counter(int start, String name) {
		super();
		this.counter = start;
		this.threadName = name;
	}

	@Override
	public void run() {
		while (counter <= 30) {
			System.out.println(counter + "=>" + threadName);
			counter += 2;
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}